<template>
  <div class="mod-card">
    <el-form :inline="true" :model="searchForm" @keyup.enter.native="pageIndex = 1;getDataList()">
      <el-form-item>
        <el-input v-model="searchForm.name" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchForm.categoryId" clearable filterable placeholder="卡券分类"
                   class="width185">
          <el-option
            v-for="item in cardCategoryList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchForm.isOnSale" clearable placeholder="状态"
                   class="width185">
          <el-option
            key="0"
            label="下架"
            value="0">
          </el-option>
          <el-option
            key="1"
            label="上架"
            value="1">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button v-if="goods" @click="()=>{if(dataListSelections[0]!=undefined) $emit('input',dataListSelections[0])}" type="success">选择</el-button>
        <el-button @click="pageIndex = 1; getDataList()">查询</el-button>
        <el-button v-if="isAuth('mall:card:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('mall:card:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        <el-button v-if="isAuth('mall:card:save')" type="danger" @click="showCardAssign()" >发放卡券</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="卡券名称">
      </el-table-column>
      <el-table-column
        prop="categoryName"
        header-align="center"
        align="center"
        label="分类">
      </el-table-column>
      <el-table-column
        prop="imgUrl"
        header-align="center"
        align="center"
        label="图片">
        <template slot-scope="scope">
          <img style="height: 50%;width: 50%" @click="openImg(scope.row.imgUrl)" :src="scope.row.imgUrl"/>
        </template>
      </el-table-column>
      <el-table-column
        prop="summary"
        header-align="center"
        align="center"
        label="卡券简介">
      </el-table-column>
      <el-table-column
        prop="cardNumber"
        header-align="center"
        align="center"
        label="卡券库存">
      </el-table-column>
      <el-table-column
        prop="stint"
        header-align="center"
        align="center"
        label="卡券购买限制">
      </el-table-column>
      <el-table-column
        prop="marketPrice"
        header-align="center"
        align="center"
        label="卡券价值">
      </el-table-column>
      <el-table-column
        prop="retailPrice"
        header-align="center"
        align="center"
        label="银联支付售价">
      </el-table-column>
      <el-table-column
        prop="wechatPrice"
        header-align="center"
        align="center"
        label="微信支付售价">
      </el-table-column>
      <el-table-column
        prop="isOnSale"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isOnSale === 0" size="small" type="danger">下架</el-tag>
          <el-tag v-else-if="scope.row.isOnSale === 1" size="small" type="success">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="200"
        label="卡券秘钥">
        <template slot-scope="scope">
          <el-button v-if="isAuth('mall:card:update')" type="text" size="small" @click="showCardsn(scope.row.id)">卡券秘钥</el-button>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="200"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('mall:card:info')" type="text" size="small" @click="showDetails(scope.row.id)">查看</el-button>
          <el-button v-if="isAuth('mall:card:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button v-if="isAuth('mall:card:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          <el-button v-if="isAuth('mall:card:update')" type="text" size="small" @click="transferShow(scope.row.id)">关联店铺</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <card-transfer v-if="transferVisible" ref="cardTransfer" ></card-transfer>
    <card-assign v-if="assignVisible" ref="cardAssign" ></card-assign>
    <cardsn v-if="snVisible" ref="cardsn" ></cardsn>

  </div>
</template>

<script>
import AddOrUpdate from './card-add-or-update'
import CardTransfer from './card-transfer'
import CardAssign from './card-assign'
import Cardsn from './cardsn'

export default {
  data() {
    return {
      searchForm: {
        name: '',
        categoryId: '',
        isOnSale: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
        totalPage: 0,
        dataListSelections: [],
        cardCategoryList: [],
        addOrUpdateVisible: false,
        assignVisible: false,
        snVisible: false,
        transferVisible: false
      }
    },
    components: {
      AddOrUpdate, CardTransfer, Cardsn, CardAssign
    },
    activated () {
      this.activatedMethod();
    },
    methods: {
      activatedMethod() {
        this.snVisible = false
        this.getDataList()
        this.getCardCategoryList()
      },
      // 获取数据列表
      getDataList () {
        this.$http({
          url: '/mall/card/list',
          method: 'get',
          params: {
            'page': this.pageIndex,
            'limit': this.pageSize,
            'name': this.searchForm.name,
            'isOnSale': this.searchForm.isOnSale,
            'categoryId': this.searchForm.categoryId
          }
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.records
            this.totalPage = data.page.total
          } else {
            this.dataList = []
            this.totalPage = 0
          }
        })
      },
      getCardCategoryList () {
        this.$http({
          url: '/mall/cardcategory/queryAllSimple',
          method: 'get',
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.cardCategoryList = data.list
          } else {
            this.cardCategoryList = []
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 查看详情
      showDetails (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, true)
        })
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      transferShow (id) {
        this.transferVisible = true
        this.$nextTick(() => {
          this.$refs.cardTransfer.init(id)
        })
      },
      showCardsn (id) {
        this.snVisible = true
        this.$nextTick(() => {
          this.$refs.cardsn.init(id)
        })
      },
      showCardAssign () {
        this.assignVisible = true
        this.$nextTick(() => {
          this.$refs.cardAssign.init()
        })
      },
      // 删除
      deleteHandle (id) {
        let ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[删除]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: '/mall/card/delete',
            method: 'post',
            data: ids
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.getDataList()
            }
          })
        }).catch(() => {
        })
      }
    },
    props:{
      goods:{}
    },
    model: {
      prop: 'goods'
    }
  }
</script>
